﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Micronet.OpenWeb.Com.Controls;
using Micronet.OpenWeb.Common;
using Micronet.OpenWeb.Entity;
using Micronet.OpenWeb.Utils;
using Micronet.OpenWeb.Com.BLL;

namespace Micronet.OpenWeb.OpenWeb.Admin.Views.Shared
{
    public partial class HomeProfileDetailsEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (OwConstants.QueryString.ID.OwQsIsExist())
            {
                OpenHomeProfileDetaildv.DefaultMode = DetailsViewMode.Edit;
            }
            else
            {
                OpenHomeProfileDetaildv.DefaultMode = DetailsViewMode.Insert;
            }
            HomeProfileDetailOds.Inserted += new ObjectDataSourceStatusEventHandler(HomeProfileDetailOds_Inserted);
            HomeProfileDetailOds.Updated += new ObjectDataSourceStatusEventHandler(HomeProfileDetailOds_Updated);
        }

        void HomeProfileDetailOds_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            UniqueProcess(sender, e);
            Response.Redirect("HomeProfileDetailsAdmin.aspx");
        }

        void HomeProfileDetailOds_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            UniqueProcess(sender, e);
            Response.Redirect("HomeProfileDetailsAdmin.aspx");
        }

        protected void OwNameTitleCbb_Init(object sender, EventArgs e)
        {
            OwComboBox cbb = sender as OwComboBox;

            cbb.OwDataSourceBindFromType<OwEnum.NameTitles>(useLanguage: false);
        }


        void UniqueProcess(object sender, ObjectDataSourceStatusEventArgs e)
        {
            var IsDefaultCbb = (OwCheckBox)OpenHomeProfileDetaildv.FindControl("IsDefaultCbb");
            if (IsDefaultCbb.Checked.ToString().OwToBoolean() == true)
            {
                var ods = OwOpenHomeProfileDetailsBLL.OwGetByDynamic(isDefault: true);
                foreach (OwOpenHomeProfileDetailsEntity entity in ods)
                {
                    OwOpenHomeProfileDetailsEntity en = OwOpenHomeProfileDetailsBLL.OwGetEntity(e.ReturnValue.OwToInt64TryParse());
                    if (en.OpenHomeProfileDetailID != entity.OpenHomeProfileDetailID)
                    {

                        entity.IsDefault = false;
                        OwOpenHomeProfileDetailsBLL.OwSave(entity);

                        OwOpenHomeProfilesEntity HomeprofileEnty = OwOpenHomeProfilesBLL.OwGetEntity(en.OpenHomeProfileID);

                        HomeprofileEnty.DefaultOpenHomeProfileDetailID = en.OpenHomeProfileDetailID;
                        OwOpenHomeProfilesBLL.OwSave(HomeprofileEnty);
                    }
                }
            }


        }
    }
}